import jwt, { SignOptions, VerifyOptions } from "jsonwebtoken";
import { Permissions } from "@/model/frame/IPermissionConfig";

export default class JWTTool {
  private static secret = "testMock";

  // 这个函数只在Mock中使用
  public static getToken<T extends object = any>(
    payload: T,
    options?: SignOptions
  ): string {
    return jwt.sign(payload as object, JWTTool.secret, options);
  }

  public static webDecodeToken<T extends object = any>(
    token: string
  ): T | string {
    const _payload = jwt.decode(token);
    if (_payload && typeof _payload !== "string") {
      return _payload as T;
    } else if (typeof _payload === "string") {
      console.log("不支持此操作！");
      return _payload;
    } else {
      return "";
    }
  }

  public static mockVerify<T extends object = any>(
    token: string,
    options?: VerifyOptions
  ): T | string {
    try {
      return jwt.verify(token, JWTTool.secret, options) as T | string;
    } catch (e) {
      return `${e}`;
    }
  }
}
